package cn.sirun.com.fca.personal.center.view.impl;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.recycler.flow.BaseRecyclerFlowView;
import com.tencent.cloud.iov.router.Router;
import com.tencent.cloud.iov.util.ActivityUtils;
import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.iov.util.lifecycle.ActivityStackManager;
import com.tencent.cloud.uikit.ui.widget.IovToolbar;
import com.tencent.cloud.uikit.utils.SystemUiUtils;

import cn.sirun.com.fca.account.constant.AccountIntentConst;
import cn.sirun.com.fca.account.service.LoginManager;
import cn.sirun.com.fca.personal.center.R;
import cn.sirun.com.fca.personal.center.constant.PersonalCenterActionConst;
import cn.sirun.com.fca.personal.center.constant.PersonalCenterActivityConst;
import cn.sirun.com.fca.personal.center.constant.PersonalCenterIntentConst;
import cn.sirun.com.fca.personal.center.presenter.IPersonalOptionsPresenter;
import cn.sirun.com.fca.personal.center.presenter.impl.PersonalOptionsPresenter;
import cn.sirun.com.fca.personal.center.view.IPersonalOptionsView;

public class PersonalOptionsFlowView extends BaseRecyclerFlowView<IPersonalOptionsPresenter>
        implements IPersonalOptionsView {
    private static final String TAG = PersonalOptionsFlowView.class.getSimpleName();

    public PersonalOptionsFlowView(Activity activity) {
        super(activity);
    }

    public PersonalOptionsFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }

    @Override
    protected RecyclerView createRecyclerView() {
        RecyclerView recyclerView = super.createRecyclerView();
        recyclerView.setClipToPadding(false);
        int paddingTop = ResourcesUtils.getDimensionPixelOffset(
                R.dimen.account_layout_margin_top);
        recyclerView.setPadding(0, paddingTop, 0, 0);
        return recyclerView;
    }

    @Override
    protected void attachRecyclerView(View recyclerView) {
        setContentView(R.layout.personal_options_activity);
        FrameLayout frameLayout = getActivity().findViewById(R.id.personal_options_container);
        frameLayout.addView(recyclerView);
        setUpActionBar();
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onStart() {
        super.onStart();
        getPresenter().refreshData();
    }

    private void setUpActionBar() {
        final Activity activity = getActivity();
        IovToolbar toolbar = SystemUiUtils.showActionBar(activity,
                ResourcesUtils.getString(R.string.setting), ResourcesUtils.getDrawable(R.color.white));
    }

    @Override
    protected IPersonalOptionsPresenter createPresenter() {
        return new PersonalOptionsPresenter();
    }

    @Override
    public void turnToItemActivity(String action, int titleId) {
        if (TextUtils.isEmpty(action)) {
            return;
        }
        if (action.equals(PersonalCenterActionConst.ACTION_QUIT_APP)) {
            quitDialog();
        } else {
            Intent intent = Router.fromPath(action);
            if (action.equals(PersonalCenterActivityConst.Path.CONFIRM_PASSWORD)) {
                intent.putExtra(AccountIntentConst.FROM, PersonalCenterIntentConst.MODIFY_LOGIN_PASSWORD);
            } else if (action.equals(PersonalCenterActivityConst.Path.CHANGE_REGISTER_PHONE)) {
                if (titleId == R.string.register_telephone) {
                    intent.putExtra(AccountIntentConst.FROM, AccountIntentConst.MODIFY_REGISTER_ACCOUNT);
                } else {
                    intent.putExtra(AccountIntentConst.FROM, AccountIntentConst.MOIDFY_BIND_ACCOUNT);
                }
            }
            ActivityUtils.startActivityForResult(getActivity(), intent);
        }
    }

    private void quitDialog() {
        LayoutInflater inflater = (LayoutInflater) getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.common_dialog, null, false);

        LinearLayout layout = view.findViewById(R.id.dialog_layout);
        TextView contentView = view.findViewById(R.id.tv_title);
        TextView sureView = view.findViewById(R.id.tv_sure);
        TextView cancleView = view.findViewById(R.id.tv_tel_cancle);
        contentView.setText(ResourcesUtils.getString(R.string.quit));

        final Dialog dialog = new Dialog(getActivity(), R.style.DialogStyle);
        dialog.setCancelable(true);
        dialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        dialog.show();

        sureView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.cancel();
                LoginManager.getInstance().logout();
                ActivityStackManager.getInstance().clearStack();
            }
        });

        cancleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.cancel();
            }
        });
    }
}
